<template>
  <!--  @click="$event.stopPropagation() -->
  <div class="xtx-city" ref="target">
    <div class="select">
      <span class="placeholder" @click="toggle">{{ modelValue.fullCity }}</span>
      <span class="value"></span>
      <i class="iconfont icon-angle-down"></i>
    </div>
    <div class="option" v-show="isShow">
      <div v-if="!cityList.length" class="loading"></div>
      <template v-else>
        <span class="ellipsis" v-for="i in cityList" :key="i.code" @click="cityClick(i)">{{ i.name }}</span>
      </template>
    </div>
  </div>
</template>
<script>
import { useStore } from 'vuex'
import { onBeforeUnmount, onMounted, ref, watch } from 'vue'
import { onClickOutside } from '@vueuse/core'

export default {
  name: 'XtxCity',
  props: {
    modelValue: {
      type: Object,
      default () {
        return {}
      }
    }
  },
  setup (props, { emit }) {
    const store = useStore()
    const isShow = ref(false)
    const cityList = ref([])

    function toggle () {
      isShow.value = !isShow.value
      if (isShow.value && !cityList.value.length) {
        store.dispatch('product/getCityList').then(res => {
          // console.log(res)
          cityList.value = res
        })
      }
    }

    const target = ref(null)
    //注册全局点击事件
    // function documentClick(e){
    //   // console.log(e)
    //   isShow.value = false
    //   console.log(target.value)
    // }
    // onMounted(()=>{
    //   document.addEventListener('click',documentClick)
    // })
    // onBeforeUnmount(()=>{
    //   document.removeEventListener('click',documentClick)
    // })

    onClickOutside(target, () => {
      //当点击target外的dom区域就会执行此回调
      isShow.value = false
    })
    // 城市数据记录
    const selectInfo = {
      proviceName: '',
      proviceCode: '',
      cityName: '',
      cityCode: '',
      areaName: '',
      areaCode: '',
      fullCity: ''
    }
    const cityArr = ['provice', 'city', 'area']

    //城市选择按钮
    function cityClick (city) {
      selectInfo[cityArr[city.level] + 'Name'] = city.name
      selectInfo[cityArr[city.level] + 'Code'] = city.code
      // console.log(city)
      if (city.level < 2) return cityList.value = city.areaList
      isShow.value = false
      selectInfo.fullCity = selectInfo.proviceName + ' ' + selectInfo.cityName + ' ' + selectInfo.areaName
      // console.log(selectInfo.fullCity)
      // 发送事件通知父组件
      emit('update:modelValue', selectInfo)
      // console.log(selectInfo.fullCity)
    }

    //监听isShow为false就还原cityList
    watch(() => isShow.value, newVal => {
      if (!newVal) cityList.value = store.state.product.cityList
    })
    return {
      isShow,
      toggle,
      target,
      cityList,
      cityClick
    }
  }
}
</script>
<style scoped lang="less">
.xtx-city {
  display: inline-block;
  position: relative;
  z-index: 400;

  .select {
    border: 1px solid #e4e4e4;
    height: 30px;
    padding: 0 5px;
    line-height: 28px;
    cursor: pointer;

    &.active {
      background: #fff;
    }

    .placeholder {
      color: #999;
    }

    .value {
      color: #666;
      font-size: 12px;
    }

    i {
      font-size: 12px;
      margin-left: 5px;
    }
  }

  .option {
    width: 542px;
    border: 1px solid #e4e4e4;
    position: absolute;
    left: 0;
    top: 29px;
    background: #fff;
    min-height: 30px;
    line-height: 30px;
    display: flex;
    flex-wrap: wrap;
    padding: 10px;

    .loading {
      height: 290px;
      width: 100%;
      background: url(~@/assets/images/loading.gif) no-repeat center;
    }

    > span {
      width: 130px;
      text-align: center;
      cursor: pointer;
      border-radius: 4px;
      padding: 0 3px;

      &:hover {
        background: #f5f5f5;
      }
    }
  }
}
</style>
